<?php 
//if (! defined('BAEPATH')) exit('No direct script access allowed');           
class AjaxLibs extends MY_Controller {

    //constructor
     function __construct()
     {                                                                   
        parent::__construct();
         $this->ClearContent();              
      }

    public function index()
    {       
    }

   function ajaxGetDocGroupList()
    {
        $DocGroups = Doc_group::all();
        $List = Array();
        foreach($DocGroups as $docGroup)
        {
            array_push($List,Array('groupName'=>$docGroup->group_name
                                    ,'department'=>$docGroup->department->depart_name
                                    ,'restricted'=>$docGroup->restricted));
        }
        
        echo json_encode(array('status'=>'ok','dataList'=>$List));
    }
    function ajaxGetDocTypeList()
    {

        $DocTypes = Doc_Type::all();
        $typeList = Array();
        foreach($DocTypes as $DocType)
        {
            array_push($typeList,Array('id'=>$DocType->id,'data'=>$DocType->doc_type_desc));
        }
        
        echo json_encode(array('status'=>'ok','dataList'=>$typeList));
    }
    function ajaxGetDepartments()
    {                              
        $departments = Department::all();
        $typeList = Array();
        foreach($departments as $dept)
        {
            array_push($typeList,Array('id'=>$dept->id,'data'=>$dept->depart_name));
        }
        
        echo json_encode(array('status'=>'ok','dataList'=>$typeList));
    }        
    function ajaxAddNewDoc()
    {
        $data = array('title'=>$_POST['title'],
                      'doc_type'=>$_POST['docType'],
                      'restricted'=>$_POST['x'],
                      'rfid'=>$_POST['rfid'],
                      'doc_group_id'=>$_POST['docGroup'],
                      'notes'=>$_POST['notes'],
                      'owner_id'=>$this->user->id);
                            
       Document:AddNewDocument($data);     
    }
    function ajaxAddNewDepartment()
    {                                                                                                                
      
        $newDepts =$_POST['newDepts'];                                

        $errorArray = Array();
        foreach($newDepts as $newDept)
        {
            $result =  Department::AddNewDepartment(array('depart_name'=>$newDept,'owner_id'=>$this->user->id)); 
            if($result === false)
                array_push($errorArray,Array('key'=>$newDept,'error'=>'database error'));
            else if ($result !== true)
                array_push($errorArray,Array('key'=>$newDept,'error'=>$result));
        }
        
        if (count($errorArray) == 0)
            echo json_encode(array('status'=>'ok'));
        else
            echo json_encode(array('status'=>'failed','errorlist'=>$errorArray));            
        
    }
    
    function ajaxAddNewDocGroup()
    {
        $this->ClearContent();
        $newGroups =$_POST['newGroups'];  
                                      
        $errorArray = Array();
        foreach ($newGroups as $docGroup)
        {   
            $new = Array();
            $new['group_name'] = $docGroup['groupName'];
            $new['department_id'] = $docGroup['dept_id'];
            $new['restricted'] = $docGroup['x'];
            $new['user_id'] = $this->user->id;
            
            $result = Doc_group::AddNewGroup($new);
            if($result === false)
                array_push($errorArray,Array('key'=>$docGroup['groupName'],'dept'=>$docGroup['dept_id'], 'error'=>'database error'));
            else if ($result !== true)
                array_push($errorArray,Array('key'=>$docGroup['groupName'],'dept'=>$docGroup['dept_id'], 'error'=>$result));
                                     
                    
        }
        if (count($errorArray) == 0)
        {
            echo json_encode(array('status'=>'ok'));
        }
        else
            echo json_encode(array('status'=>'failed','errorlist'=>$errorArray));     
            //echo json_encode(array('status'=>'failed','error'=>'error adding new document group'));     

        
        
    }
    function ajaxAddNewDocTypes()
    {
        
        $this->ClearContent();
        $newTypes =$_POST['newTypes'];                                

        $errorArray = Array();
        foreach($newTypes as $newType)
        {
            $result = Doc_Type::AddNewType($newType);
            if($result === false)
                array_push($errorArray,Array('key'=>$newType,'error'=>'database error'));
            else if ($result !== true)
                array_push($errorArray,Array('key'=>$newType,'error'=>$result));
        }
        
        if (count($errorArray) == 0)
            echo json_encode(array('status'=>'ok'));
        else
            echo json_encode(array('status'=>'failed','errorlist'=>$errorArray));     
            
    }
    

}/* End of class */

?>